package com.cheyun.netsalev3.viewmodel.orderquanyi

import android.content.Intent
import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import androidx.paging.PagedList
import com.cheyun.netsalev3.bean.orderquanyi.OrderQuanyiData
import com.cheyun.netsalev3.repository.aftersale.MaintenanceDetailsRepository
import com.cheyun.netsalev3.repository.orderquanyi.OrderQuanyiRepository
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.view.MyScanActivity
import com.cheyun.netsalev3.view.orderquanyi.OrderQuanyiChildActivity
import com.cheyun.netsalev3.viewmodel.BaseViewModel
import com.jeremyliao.liveeventbus.LiveEventBus

/**
 * @author Michael
 * @date  2018/10/30 - 15:55.
 */
class OrderQuanyiViewModel(var repository: HashMap<String, OrderQuanyiRepository>) : BaseViewModel() {
    var pagedlist = HashMap<String, LiveData<PagedList<OrderQuanyiData>>>()
    var hashMap = HashMap<String, LiveData<NetworkState>>()
    var totlelist = HashMap<String, LiveData<Int>>()

    var loginData = MySharedPreferences.getLoginData()

    var maintenanceDetailsRepository = MaintenanceDetailsRepository(0)

    //点击保养列表
    fun onClickBaoyang(view: View, data: OrderQuanyiData) {
        var intent = Intent(view.context, OrderQuanyiChildActivity::class.java)
        intent.putExtra("tid",data.orderId.toString())
        view.context.startActivity(intent)
    }


    init {
        for (key in repository.keys) {
            repository[key]?.apply {
                pagedlist[key] = this.loadAndroidData(null)
            }
        }
    }

    init {
        for (item in repository.keys) {
            pagedlist[item]?.apply {
                hashMap[item] = Transformations.switchMap(this) { repository[item]!!.networkState }!!
                totlelist[item] = Transformations.switchMap(this) { repository[item]!!.total }!!
            }
        }
    }

    fun initData(nkey: String) {
        repository[nkey]?.apply {
            this.setEanbleLoad(true)
            this.dataSourceFactory.sourceLiveData.value?.invalidate()
        }
    }

    /**
     * 刷新
     */
    fun refresh(nkey: String,hashMap:HashMap<String, String>? = null) {
        if(hashMap != null) {
            repository[nkey]?.changeMap(hashMap)
        }
        repository[nkey]?.dataSourceFactory?.sourceLiveData?.value?.invalidate()
    }

    fun getProcess(item:OrderQuanyiData):String{
        return "核销："+item.now+"/" + item.total
    }


}